/**
 * author levi
 * url http://levi.cg.am
 */
var imgkey;
var reurl = /^http(.*)/;
KISSY.use('gallery/uploader/1.4/index,gallery/uploader/1.4/themes/default/index,gallery/uploader/1.4/themes/default/style.css', function (S, Uploader,DefaultTheme) {
        //上传组件插件
        var plugins = 'gallery/uploader/1.4/plugins/auth/auth,' +
                'gallery/uploader/1.4/plugins/urlsInput/urlsInput,' +
                'gallery/uploader/1.4/plugins/proBars/proBars';

        S.use(plugins,function(S,Auth,UrlsInput,ProBars){
            var uploader = new Uploader('.file-input',{
                //处理上传的服务器端脚本路径
				"name":"userfile",
				"action":"/services/img/apiupload/SaveImg.php"
            });
            //使用主题
            uploader.theme(new DefaultTheme({
                "queueTarget":'#J_UploaderQueue'
            }))
                    //验证插件
            uploader.plug(new Auth({
                        //最多上传个数
                        "max":3,
                        //图片最大允许大小
                        "maxSize":2000,
                        "allowExts":"png,jpg,gif"
                    }));
                     //url保存插件
             uploader.on('complete', function (ev) {
				var index = ev.index, file = ev.file;
                //服务器端返回的结果集
                var result = ev.result;
				var img = "<img src=\""+result.url+"_120x120.jpg\" />";
				if(imgkey==4){
					$(".uploadbg").val(result.url);
					$(".bg_own").val(result.url);
					$("#area").css("background","url("+result.url+") no-repeat");
					savepiccfg(1);
				}else if (imgkey==3){
					$(".cart-pic").val(result.url);
					$("#cart").css("background","url("+result.url+") no-repeat");
				}else if (imgkey==1){
					$(".action-pic").val(result.url);
					$("#action").css("background","url("+result.url+") no-repeat");
				}
				// $(".upImage"+imgkey).empty();
				// $(img).appendTo(".upImage"+imgkey);
				$(".imgurl"+imgkey).val(result.url);
				$(".form").change();
				// $(".imgurl"+imgkey).fire("change");
            });
			uploader.on('error', function (ev) {
                var index = ev.index, file = ev.file;
                //服务器端返回的结果集
                var result = ev.result;
				showDialog("上传出错", '上传失败,错误消息为：' +result.msg,"form-horizontal");
            });

                    //进度条集合
            uploader.plug(new ProBars());
            ;
        });
    });
function saveanimcfg(status){
		KISSY.io({
			url: "/?a=anmicfg_save&m=shopadmin&c=frontcfg",
			data: {"status":status},
			success: function(data, textStatus) {
				if(data.status==1){
					if(data.anmicfg==1){
						$(".js-animon").addClass("button-danger");
						$(".js-animoff").removeClass("button-danger");
						$(".icon-repeat").addClass("icon-white");
						$(".icon-ban-circle").removeClass("icon-white");
					}else{
						$(".js-animoff").addClass("button-danger");
						$(".js-animon").removeClass("button-danger");
						$(".icon-repeat").removeClass("icon-white");
						$(".icon-ban-circle").addClass("icon-white");
					}
				}else{
					alert("配置失败,请确认您已经配置活动。");
				}
			},
			complete: function(data){
			},
			error: function(){
				alert('error');
			},
			dataType:"jsonp"
		});
	}
function savepiccfg(status){
	KISSY.io({
		url: "/?a=gamewg_save_kg&m=shopadmin&c=frontcfg",
		data: {"status":status,"picurl":$(".bg_own").val(),"eid":$(".eid").val()},
		success: function(data, textStatus) {
			if(data.status==1){
				if(data.front_pic==1){
					$(".js-open").addClass("button-danger");
					$(".js-open").html("<i class=\"icon-white icon-eye-open \"></i>已启用");
					$("#area").css("background-image","url("+$(".bg_own").val()+")");
				}else{
					$(".js-open").removeClass("button-danger");
					$(".js-open").html("<i class=\"icon-eye-close \"></i>开启");
					$("#area").css("background-image","url("+$(".bg_default").val()+")");
				}
			}else{
				alert("上传出错！");
			}
		},
		complete: function(data){
		},
		error: function(){
			alert("上传出错！");
		},
		dataType:"jsonp"
	});
}
$(function() {
	$(".js-animon").click(function(){
		saveanimcfg(1);
	});
	$(".js-animoff").click(function(){
		saveanimcfg(0);
	});
	$(".js-imgup").click(function(){
		// $("#J_UploaderQueue").empty();
		imgkey = $(this).attr("data-imgkey");
		$(".file-input").click();
	});
	$(".js-open").click(function(){
		if(!$(this).hasClass("button-danger")){
			savepiccfg(1);
			loaddata("data-saved");
			loadspan1("default");
		}else{
			savepiccfg(0);
			loaddata("data-default");
			loadspan1("saved");
		}
	});
	var mem = new Array();
	var mst = 0;
	var did = 0;
	var moveid;
	$(document).keydown(function(event) {
		if(event.ctrlKey&&event.which == 90){
			if(mst>0){
				var key = --mst;
				// $(".breadcrumb").text("key:"+key+"mst:"+mst);
				if(mem[key]["width"]){
					$(".width"+mem[key]["id"]).val(mem[key]["width"]);
					$(".height"+mem[key]["id"]).val(mem[key]["height"]);
					$(".dd"+mem[key]["id"]).css({"width":mem[key]["width"]});
					$(".dd"+mem[key]["id"]).css({"height":mem[key]["height"]});
				}
				if(mem[key]["top"]){
					$(".top"+mem[key]["id"]).val(mem[key]["top"]);
					$(".left"+mem[key]["id"]).val(mem[key]["left"]);
					$(".dd"+mem[key]["id"]).css({"top":mem[key]["top"]});
					$(".dd"+mem[key]["id"]).css({"left":mem[key]["left"]});
				}
				if(mem[key]["display"]){
					if(mem[key]["display"]=="none"){
						$(".fb"+mem[key]["id"]).prop("checked",false);
						$(".dd"+mem[key]["id"]).css({"display":"normal"});
					}else{
						$(".fb"+mem[key]["id"]).prop("checked",true);
						$(".dd"+mem[key]["id"]).css({"display":"none"});
					}
				}
			}
			// Array.remove(mem[mst]);
		}
	});
	$(document).mousemove(function(e) {
		if (!!this.move) {
			var id = $(this.move_target).attr("id");
			var posix = !document.move_target ? {'x': 0, 'y': 0} : document.move_target.posix,
				callback = document.call_down || function() {
					$(".top"+id).val(e.pageY - posix.y);
					$(".left"+id).val(e.pageX - posix.x);
					$(this.move_target).css({
						'top': e.pageY - posix.y,
						'left': e.pageX - posix.x
					});
				};
			callback.call(this, e, posix);
		}
	}).mouseup(function(e) {
		if (!!this.move) {
			var id = $(this.move_target).attr("id");
			if(!id){
				id = moveid;
			}
			// $(".breadcrumb").text(mst+"moved id:"+id);
			mem[mst]={
				'id':id,
				'width': $(".dd"+id).width(),
				'height': $(".dd"+id).height(),
				'top': $(".dd"+id).position().top,
				'left': $(".dd"+id).position().left
			};
			var callback = document.call_up || function(){};
			callback.call(this, e);
			$.extend(this, {
				'move': false,
				'move_target': null,
				'call_down': false,
				'call_up': false
			});
		}
	});
	$(".coorx").mouseup(function(e){
		var id = $(this).attr("data-id");
		$(".dd"+id).css({"display":"none"});
		$(".fb"+id).prop("checked",true);
		mem[mst]={
			'id':id,
			'display': "none",
		};
	});
	$(".forb").mouseup(function(e){
		var id = $(this).attr("data-id");
		if($(this).prop("checked")==true){
			$(".dd"+id).css({"display":"normal","visibility":"visible"});
			$(".dd"+id).show();
			// $("#"+id).css({"visibility":"hidden"});
			mem[mst]={
				'id':id,
				'display': "normal",
			};
		}else{
			$(".dd"+id).css({"display":"none","visibility":"hidden"});
			// $("#"+id).css({"visibility":"visible"});
			mem[mst]={
				'id':id,
				'display': "none",
			};
		}
		mst++;
	});
	var $box = $('.dicesdiv,.edit').mousedown(function(e) {
	    var offset = $(this).position();
	    this.posix = {'x': e.pageX - offset.left, 'y': e.pageY - offset.top};
	    $.extend(document, {'move': true, 'move_target': this});
		var id = $(this).attr("id");
		// alert(mst);
		if(mst==0||mem[mst]["id"]!=id){
			mem[mst]={
				'id':id,
				'width': $(".dd"+id).width(),
				'height': $(".dd"+id).height(),
				'top': $(".dd"+id).position().top,
				'left': $(".dd"+id).position().left
			};
			// mst++;
		}
		if($(this).hasClass("dicesdiv")){
			$(".kgopen").attr("checked",true);
		}
		// $(".breadcrumb").text(mst+"mousedown"+id);

	}).on('mousedown', '#coor', function(e) {
		var id = $(this).attr("data-id");
		var ext = $(this).attr("data-ext");
	    var posix = {
	            'w': $(".dd"+id).width(), 
	            'h': $(".dd"+id).height(), 
	            'x': e.pageX, 
	            'y': e.pageY
	        };
		if(mst==0||mem[mst]["id"]!=id){
			mem[mst]={
				'id':id,
				'width': posix.w,
				'height': posix.h,
				'top': $(".dd"+id).position().top,
				'left': $(".dd"+id).position().left
			};
		}
	    $.extend(document, {'move': true, 'call_down': function(e) {
			var width = Math.max(30, e.pageX - posix.x + posix.w);
			var height = Math.max(30, e.pageY - posix.y + posix.h);
			$(".width"+id).val(width);
			$(".height"+id).val(height);
			var style;
			if(ext){
				style = {
					'width': width,
					'height': height,
					"line-height": height+"px",
				};
			}
			else
			{
				style = {
					'width': width,
					'height': height,
				};
			}
	        $(".dd"+id).css(style);
			// $(".breadcrumb").text(mst+"change"+width+height);

	    }});
	    return false;
	}).mouseup(function(e) {
		var id = $(this).attr("id");
		moveid = id;
		mst++;
		dojson();
	});
	$(".js-wgsubmit").mouseup(function(){
		var t1=$(".forb");
		t1.each(function(index){
			if($(this).prop("checked") == false){
				$(this).prop("checked",true);
				$(this).val(0);
			}
		});
		$(".form1").submit();
	});
	$(".js-back").mouseup(function(){
		location.href="/?a=gamecfg&m=shopadmin";
	});
	$(".span1").change(function(){
		// if(!typeof($(this).attr("data-id"))=="undefined"){
			var id = $(this).attr("data-id");
			var k = $(this).attr("data-ty");
			var v = $(this).val();
			$(".dd"+id).css(k,v+"px");
		// }
	});
	$(".bui-tab-item").on("click",function(){
		var tp = $(this).attr("data-tp");
		if(tp=="bag_set"){
			$(".jinnang").show();
		}else{
			$(".jinnang").hide();
		}
	});
	$(".form").change(function(){
		dojson();
		if($(this).hasClass("leftcontdown")){$("#contdown").css({"left":$(this).val()});}
		if($(this).hasClass("topcontdown")){$("#contdown").css({"top":$(this).val()});}
		if($(this).hasClass("leftgift")){$("#gift").css({"left":$(this).val()});}
		if($(this).hasClass("topgift")){$("#gift").css({"top":$(this).val()});}
		if($(this).hasClass("leftmywin")){$("#mywin").css({"left":$(this).val()});}
		if($(this).hasClass("topmywin")){$("#mywin").css({"top":$(this).val()});}
		if($(this).hasClass("leftgg")){$("#gg").css({"left":$(this).val()});}
		if($(this).hasClass("topgg")){$("#gg").css({"top":$(this).val()});}
		if($(this).hasClass("leftwin")){$("#win").css({"left":$(this).val()});}
		if($(this).hasClass("topwin")){$("#win").css({"top":$(this).val()});}
		if($(this).hasClass("leftaction")){$("#action").css({"left":$(this).val()});}
		if($(this).hasClass("topaction")){$("#action").css({"top":$(this).val()});}
		if($(this).hasClass("widthaction")){$("#action").css({"width":$(this).val()});}
		if($(this).hasClass("heightaction")){$("#action").css({"height":$(this).val()});}
		if($(this).hasClass("action-pic")){
			if(reurl.test($(this).val())){
				$("#action").css({"background":"url("+$(this).val()+") no-repeat"});
			}else{
				$("#action").css({"background":$(this).val()});
			}
		}
		
		if($(this).hasClass("leftcart")){$("#cart").css({"left":$(this).val()});}
		if($(this).hasClass("topcart")){$("#cart").css({"top":$(this).val()});}
		if($(this).hasClass("widthcart")){$("#cart").css({"width":$(this).val()});}
		if($(this).hasClass("heightcart")){$("#cart").css({"height":$(this).val()});}
		if($(this).hasClass("cart-pic")){
			if(reurl.test($(this).val())){
				$("#cart").css({"background":"url("+$(this).val()+") no-repeat"});
			}else{
				$("#cart").css({"background":$(this).val()});
			}
		}
	});
	$(".js-readdefault").click(function(){
		loaddata("data-default");
		loadspan1("saved");
	});
	$(".js-readloaded").click(function(){
		loaddata("data-saved");
		loadspan1("default");
	});
	$(".js-read").click(function(){
		var tp = $(this).attr("data-do");
		loadspan1(tp);
	});
	$("select").change(function(){
		$(".kaiguan").click();
	});
	function loadspan1(ac){
		$(".span1").each(function(index){
			if(ac=="default"){
				$(this).val($(this).attr("data-default"));
			}else{
				$(this).val($(this).attr("data-saved"));
			}
		});
		$(".span1").change();
	}
	function loaddata(data){
		// var data = "data-default";
		// alert(data);
		$(".form").each(function(index){
			var set_data = $(this).attr(data);
			
			if($(this).attr("type")=="checkbox"){
				if(set_data=="hidden"){
					$(this).attr("checked","checked");
				}else{
					$(this).attr("checked",null);
				}
			}else{
				$(this).val(set_data);
			}
		});
		
		$(".contdown-left").val($(".contdown-left").attr(data));
		$(".contdown-top").val($(".contdown-top").attr(data));
		$(".contdown-hide").attr(data)=="visible"?$(".contdown-hide").prop("checked",false):$(".contdown-hide").prop("checked",true);
		$("#contdown").css({"left":$(".contdown-left").attr(data)+"px","top":$(".contdown-top").attr(data)+"px","visibility":$(".contdown-hide").attr(data),"display":"normal"});
		
		$(".gift-right").val($(".gift-right").attr(data));
		$(".gift-top").val($(".gift-top").attr(data));
		$(".gift-hide").attr(data)=="visible"?$(".gift-hide").prop("checked",false):$(".gift-hide").prop("checked",true);
		$("#gift").css({"left":$(".gift-right").attr(data)+"px","top":$(".gift-top").attr(data)+"px","visibility":$(".gift-hide").attr(data),"display":"normal"});

		$(".mywin-right").val($(".mywin-right").attr(data));
		$(".mywin-top").val($(".mywin-top").attr(data));
		$(".mywin-hide").attr(data)=="visible"?$(".mywin-hide").prop("checked",false):$(".mywin-hide").prop("checked",true);
		$("#mywin").css({"left":$(".mywin-right").attr(data)+"px","top":$(".mywin-top").attr(data)+"px","visibility":$(".mywin-hide").attr(data),"display":"normal"});

		$(".gg-right").val($(".gg-right").attr(data));
		$(".gg-top").val($(".gg-top").attr(data));
		$(".gg-hide").attr(data)=="visible"?$(".gg-hide").prop("checked",false):$(".gg-hide").prop("checked",true);
		$("#gg").css({"left":$(".gg-right").attr(data)+"px","top":$(".gg-top").attr(data)+"px","visibility":$(".gg-hide").attr(data),"display":"normal"});

		$(".win-right").val($(".win-right").attr(data));
		$(".win-bottom").val($(".win-bottom").attr(data));
		$(".win-hide").attr(data)=="visible"?$(".win-hide").prop("checked",false):$(".win-hide").prop("checked",true);
		$("#win").css({"left":$(".win-right").attr(data)+"px","top":$(".win-bottom").attr(data)+"px","visibility":$(".win-hide").attr(data),"display":"normal"});

		$(".action-left").val($(".action-left").attr(data));
		$(".action-top").val($(".action-top").attr(data));
		$(".action-width").val($(".action-width").attr(data));
		$(".action-height").val($(".action-height").attr(data));
		$("#action").css({"left":$(".action-left").attr(data)+"px",
						  "top":$(".action-top").attr(data)+"px",
						  "width":$(".action-width").attr(data)+"px",
						  "height":$(".action-height").attr(data)+"px",
						  "visibility":"visible",
						  "background":$(".action-pic").attr(data)});
		
		var pic = $(".action-pic").attr(data);
		$(".action-pic").val(pic);
		
		$(".cart-left").val($(".cart-left").attr(data));
		$(".cart-top").val($(".cart-top").attr(data));
		$(".cart-width").val($(".cart-width").attr(data));
		$(".cart-height").val($(".cart-height").attr(data));
		$(".cart-hide").attr(data)=="visible"?$(".cart-hide").prop("checked",false):$(".cart-hide").prop("checked",true);
		$("#cart").css({"left":$(".cart-left").attr(data)+"px",
					  "top":$(".cart-top").attr(data)+"px",
					  "width":$(".cart-width").attr(data)+"px",
					  "height":$(".cart-height").attr(data)+"px",
					  "visibility":$(".cart-hide").attr(data)});
		$(".cart-pic").val($(".cart-pic").attr(data));
		dojson();
	}
	dojson();
	function dojson(){
		var contdown={};
		var gift={};
		var mywin={};
		var gg={};
		var win={};
		var action={};
		var cart={};
		contdown.left = $(".contdown-left").val()+"px";
		contdown.top = $(".contdown-top").val()+"px";
		contdown.visibility = $(".contdown-hide").prop("checked")?"hidden":"visible";
		
		gift.left = $(".gift-right").val()+"px";
		gift.top = $(".gift-top").val()+"px";
		gift.visibility = $(".gift-hide").prop("checked")?"hidden":"visible";

		mywin.left = $(".mywin-right").val()+"px";
		mywin.top = $(".mywin-top").val()+"px";
		mywin.visibility = $(".mywin-hide").prop("checked")?"hidden":"visible";

		gg.left = $(".gg-right").val()+"px";
		gg.top = $(".gg-top").val()+"px";
		gg.visibility = $(".gg-hide").prop("checked")?"hidden":"visible";

		win.left = $(".win-right").val()+"px";
		win.top = $(".win-bottom").val()+"px";
		win.visibility = $(".win-hide").prop("checked")?"hidden":"visible";

		action.left = $(".action-left").val()+"px";
		action.top = $(".action-top").val()+"px";
		action.width = $(".action-width").val()+"px";
		action.height = $(".action-height").val()+"px";
		var pic = $(".action-pic").val();
		if(reurl.test(pic)){
			action.background = "url("+$(".action-pic").val()+") no-repeat";
		}else{
			action.background = $(".action-pic").val();
		}

		cart.left = $(".cart-left").val()+"px";
		cart.top = $(".cart-top").val()+"px";
		cart.width = $(".cart-width").val()+"px";
		cart.height = $(".cart-height").val()+"px";
		cart.background = "url("+$(".cart-pic").val()+") no-repeat";
		cart.visibility = $(".cart-hide").prop("checked")?"hidden":"visible";
		var code = {"contdown":contdown,"gift":gift,"mywin":mywin,"gg":gg,"win":win,"action":action,"cart":cart};
		$(".js-code").val(JSON.stringify(code));
	}
	
	function __dialog(title,addon,top,width){
			var tstr = top?"top:"+top+"px;":"";
			var wstr = width?"width:"+width+"px;":"width:830px";
			var dialog =("<div class=\"bui-dialog\" style=\""+wstr+"visibility:visible;display:block;"+tstr+"\">");
			dialog+=("    <div class=\"bui-stdmod-header\">");
			dialog+=("        <div class=\"header-title\">");
			dialog+=("            "+title+"");
			dialog+=("        </div>");
			dialog+=("    </div>");
			dialog+=("    <div class=\"bui-stdmod-body\">");
			dialog+=("    "+addon);
			dialog+=("    </div>");
			dialog+=("    <div class=\"bui-stdmod-footer\">");
			dialog+=("        <button class=\"button button-primary cancel\">");
			dialog+=("            关闭");
			dialog+=("        </button>");
			dialog+=("    </div>");
			dialog+=("    <a class=\"bui-ext-close\">");
			dialog+=("        <span class=\"bui-ext-close-x x-icon x-icon-normal\">");
			dialog+=("            ×");
			dialog+=("        </span>");
			dialog+=("    </a>");
			dialog+=("</div>");
			return dialog;
	}
	function showDialog(title,addon,show,top){
		top=top?top:KISSY.DOM.scrollTop()+KISSY.DOM.viewportHeight()/2-100;
		var width=($("."+show).innerWidth());
		var mask = ("<div style=\"width:100%;left:0;top:0;height:100%;position:fixed;\" class=\"bui-ext-mask\"></div>");
		$(mask).insertBefore("."+show);
		$(__dialog(title,addon,top,width)).insertBefore("."+show);
		$(".bui-ext-close-x").on("click",function(){$(".bui-ext-mask").remove();$(".bui-dialog").remove();$(".doc-content").removeClass("height240");});	
		$(".cancel").on("click",function(){$(".bui-ext-mask").remove();$(".bui-dialog").remove();$(".doc-content").removeClass("height240");});	
		// $(".bui-dialog").scrollIntoView();
	}
	function closeDialog(){
	$(".bui-ext-close-x").fire("click");
	}
	$(".helper").click(function(){
		showDialog("帮助",$(this).attr("data-tip"),"form-horizontal");
		$(".helper-in").on("click",function(){
			closeDialog();
			showDialog("帮助",$(this).attr("data-tip"),"form-horizontal");
		});
	});
	$(".bui-tab-item").on("click",function(){
		var show = $(this).attr("data-tp");
		$(".mytab").hide();
		$(".js-"+show).show();
		$(".bui-tab-item").removeClass("active");
		$(this).addClass("active");
	});
	$(".js-btn_tp").on("click",function(){
		var id = $(this).attr("data-id");
		var tp = $(this).attr("data-tp");
		$("."+id).hide();
		$("."+id+tp+"_div").show();
		$("."+id+"btn_set").val(tp);
	});
	$(".js-btn_siz").on("click",function(){
		var id = $(this).attr("data-id");
		var tp = $(this).attr("data-tp");
		var dom = "."+id+"btn";
		$(dom).removeClass("btn-mini");
		$(dom).removeClass("btn-small");
		$(dom).removeClass("btn-large");
		$(dom).addClass("btn-"+tp);
		$("."+id+"btn_size").val("btn-"+tp);
	});
	$(".js-btn_style").on("click",function(){
		var id = $(this).attr("data-id");
		var tp = $(this).attr("data-tp");
		var dom = "."+id+"btn";
		$(dom).removeClass("btn-primary");
		$(dom).removeClass("btn-warning");
		$(dom).removeClass("btn-danger");
		$(dom).removeClass("btn-success");
		$(dom).removeClass("btn-info");
		$(dom).removeClass("btn-inverse");
		$(dom).addClass("btn-"+tp)
		$("."+id+"btn_style").val("btn-"+tp);
	});
	$(".qdbtn").on("click",function(){return false;});
});